<?php
/**
 *   Joomdiet - administrator/components/models/editphysicalactivity.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class JoomdietModelEditPhysicalActivity extends JModel
{
    var $_physicalactivity;
    
    function __construct()
    {
        parent::__construct();
        
        $array = JRequest::getVar('cid', 0, '', 'array');
        $this->setId((int)$array[0]);
    }
    
    function setId($id)
    {
        $this->_id = $id;
        $this->_physicalactivity = null;
    }
    
    function get($property, $default=null)
    {
        if ($this->_loadPhysicalActivity()) {
            if (isset($this->_physicalactivity->$property)) {
                return $this->_physicalactivity->$property;
            }
        }
        
        return $default;
    }
    
    function &getPhysicalActivity()
    {
        if ($this->_loadPhysicalActivity()) {
        } else $this->_initPhysicalActivity();
        
        return $this->_physicalactivity;
    }
    
    function _loadPhysicalActivity()
    {
        if (empty($this->_physicalactivity))
        {
            $query = 'SELECT * FROM #__joomdiet_Physical_Activity WHERE id='.$this->_id;
            $this->_db->setQuery($query);
            $this->_physicalactivity = $this->_db->loadObject();
            
            return (boolean) $this->_physicalactivity;
        }
    }
    
    function _initPhysicalActivity()
    {
        if (empty($this->_physicalactivity)) {
            $physicalactivity = new stdClass();
            $physicalactivity->id = 0;
            $physicalactivity->name = null;
            $physicalactivity->energy = null;
            $physicalactivity->category_tag = null;
            
            $this->_physicalactivity = $physicalactivity;
            return (boolean) $this->_physicalactivity;
        }
        return true;
    }
    
    function store()
    {
        $row =& JTable::getInstance('physicalactivity', 'Table');

        $id = JRequest::getString('physical_activity_id');
        $name = JRequest::getString('physical_activity_name');
        $energy = JRequest::getString('physical_activity_energy');
        $category_tag = JRequest::getString('physical_activity_category_tag');

        if ($id) $row->set('id', $id);
        $row->set('name', $name);
        $row->set('energy', $energy);
        $row->set('category_tag', $category_tag);
        
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        
        return true;
    }
}
